# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pandas as pd
import os
import json
class FontworksPipeline:
    #定义爬虫数据存放的文件名
    def __init__(self):
        self.file_name='FontWorks字体.json'   

    #在爬虫文件开始前执行打开
    def open_spider(self,spider):
        self.fp=open(self.file_name,'w',encoding='utf-8')
    
    '''
    #字典
    def process_item(self, item, spider):
        #写入字典
        self.fp.write(str(item))
        return item
    '''

    #json
    def process_item(self, item, spider):

        item_json = json.dumps(dict(item), ensure_ascii=False)
        self.fp.write(item_json+ '\n')
        return item        
        
    #在爬虫前执行后关闭，并创建xlsx
    def close_spider(self,spider):
        self.fp.close()
        
    #用pandas将json文件转为xlsx
        path=os.getcwd()+"\\"+self.file_name
        file=open(path,'r',encoding="utf8")
        font_json_list=[]
        df=pd.DataFrame()
        #一行一行读取json并加入list，否则会报错
        for line in file.readlines():
            dic=json.loads(line)
            font_json_list.append(dic)
        #DataFrame拼接
        for i in range(0,len(font_json_list)):
            df=pd.concat([df,pd.DataFrame(font_json_list[i])])
        
        #重设索引，drop=True表示删除原索引
        df=df.reset_index(drop=True)
        df.to_excel(os.getcwd()+r"\字体.xlsx")
